<?php

declare(strict_types=1);

namespace App\CustHub\Work;

use App\CustHub\Interfaces\OpenWorkApplicationInterface;
use App\CustHub\Mapper\ChOpenWorkAgentMapper;
use EasyWeChat\OpenWork\Message;
use Hyperf\Di\Annotation\Inject;

class TxlAgentService
{
    #[Inject]
    protected OpenWorkApplicationInterface $weWork;

    #[Inject]
    protected ChOpenWorkAgentMapper $agentMapper;

    public function saveAuth(Message $message)
    {
        $suiteId = $message->SuiteId;
        $authCode = $message->AuthCode;

        $app = $this->weWork->getTxlWork();

        $suiteAccessToken = $this->weWork->getTxlSuiteAccessToken($app->getAccount()->getSuiteId(),$app->getAccount()->getSuiteSecret());
        $result = $this->weWork->getPermanentCode($suiteAccessToken->getToken(),$authCode);
        $openUserId = '';
        if(!empty($result['auth_user_info'])){
            $openUserId = $result['auth_user_info']['open_userid'];
        }

        $permanentCode = $result['permanent_code'];
        $authCorpInfo = $result['auth_corp_info'];

        $corpid = $authCorpInfo['corpid'];

        $agentInfo = $this->agentMapper->getInfoByCorpid($corpid);
        //todo 需要新增 编辑 授权信息
    }
}